package com.zcxy360.sandtable.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImgUtil {

	public static ByteArrayOutputStream thumbnailImage(File imgFile, Integer w, Integer h) throws IOException {
		String suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".")+1);
		ByteArrayOutputStream b = new ByteArrayOutputStream();
		BufferedImage img = ImageIO.read(imgFile);
		int width = img.getWidth();
		int height = img.getHeight();
		if(w == null){
			ImageIO.write(img, suffix, b);
			return b;
		}
		if( h == null){
			h = (int)((double)height/width*w);
		}
		BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.getGraphics();
		g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
		g.dispose();
		ImageIO.write(bi, suffix, b);
		return b;
	}

}
